$(function() {
	// 获取首页信息
	var loading = weui.loading('loading', {
		className: 'custom-classname',
	});
	// 获取最新列表
	var result = Model.gift.index({
		index: 0,
		size: 0,
	});
	if (result.code) {
		Common.fn.formatOutput(result);
		loading.hide();
		return false;
    }
    // 获取顶部广告
	for (var i = 0; i < result.data[0].adver_info.length; i++) {
        var item = '',
            res = result.data[0].adver_info[i];
        if(res.businessId != '') {
            // 私人广告
            item += '<a href="../views/shop_detail.html?id=' + res.businessId + '" class="swiper-slide"><img class="lazy-img" src="' + res.picture + '" /></a>';
        }else {
            // 官方广告
            item += '<a href="javascript:;" class="swiper-slide"><img class="lazy-img" src="' + res.picture + '" /></a>';
        }
        $('.swiper-wrapper').append(item);
    }
    // 获取礼品信息
    for(i = 0;i < result.data[0].gift_info.length;i++) {
        var item = '',
            res = result.data[0].gift_info[i];
        item += '<a href="gift_detail.html?id=' + res.id + '" class="weui-media-box weui-media-box_appmsg inte-index-list"><div class="weui-media-box__hd"><img class="weui-media-box__thumb lazy-img" src="' + res.picture + '" alt=""></div><div class="weui-media-box__bd"><h4 class="weui-media-box__title">' + res.name + '</h4><p class="weui-media-box__desc">';
        if(res.free == '不包邮') {
            item += '邮费 ' + res.postage;
        }else {
            item += res.free;
        }
        item += '</p><p class="weui-media-box__desc">剩余：' + res.number + '</p></div><span class="inte-index-list-price">' + res.price + '红米</span></a>'
        $('.m-container').append(item);
    }
    // 设置红米数量
    $('.topPrice').text(result.data[0].redRice);
    // 提示红米
    weui.alert('分享礼品商城至朋友圈后获得十个红米，每天可以分享三次。');
    var mySwiper = new Swiper('.swiper-container', {
		direction: 'horizontal',
		speed: 2000,
        loop: true,
        autoplay:true,
		// 如果需要分页器
		pagination: { el: '.swiper-pagination' },
    })
    // 随机图片
    var adPicLens = result.data[0].adver_info.length,
        rand = Math.floor(Math.random() * adPicLens);
    Model.wechat.giftWeShare({
		gid: 0,
		info: '!',
		link: 'http://www.hongmuwenwan.com/web/integralShop/link.html?link=https://mp.weixin.qq.com/s/uuXNK4e4VGHi_1hcpifH1A',
		isIndex: true,
		img: result.data[0].adver_info[rand].picture,
	})
    // 图片懒加载
	lazyImg();
	loading.hide();
});

// 图片懒加载
function lazyImg() {
	$('.lazy-img').lazyload({
		effect: 'fadeIn',
	});
}
